package fr.maistre.SystemeSauvegarde;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;


import android.content.Context;

public class SaveGameManager {
	
	private Context context;
	String saveFileName = "save.srl";
	
	
	public SaveGameManager(Context appContext) {
		this.context = appContext;
	}
	
	public boolean saveExist(){
		try {
			context.openFileInput(saveFileName);
			return true;
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
			return false;
		}
	}

	public void sauvegarder(Serializable serializable){
		
		try {
			final ObjectOutputStream oos = new ObjectOutputStream(context.openFileOutput("save.srl", Context.MODE_PRIVATE));
			oos.writeObject(serializable);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Serializable load() {
		 // ouverture d'un flux sur un fichier
		try {
			ObjectInputStream ois =  new ObjectInputStream(context.openFileInput(saveFileName)) ;
			try {
				return (Serializable) ois.readObject();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (StreamCorruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}
}
